package com.seashop.util;

import org.apache.commons.lang3.RandomUtils;

import java.util.*;

/**
 * @author moredln
 * @desc
 * @date 2020-08-11 17:19
 */
public class RandomListUtil {

    /**
     * 打乱列表实现方法
     *
     * @param list
     * @param <T>
     */
    public static <T> void shuffle(List<T> list) {
        int size = list.size();
        Random random = new Random();

        for (int i = 0; i < size; i++) {
            // 获取随机位置
            int randomPos = random.nextInt(size);
            // 当前元素与随机元素交换
            T temp = list.get(i);
            list.set(i, list.get(randomPos));
            list.set(randomPos, temp);
        }
    }

    /**
     * 测试随机list
     *
     * @param args
     */
    public static void main(String[] args) {
        List<Map> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Map map = new HashMap();
            map.put("id", i);
            map.put("name", "lijie" + i);
            list.add(map);
        }
        shuffle(list);
        System.out.println(list);
        System.out.println(String.valueOf(System.currentTimeMillis() / 1000) + RandomUtils.nextInt(0, 10000));
    }
}
